package com.huarui.purchaseOrder.service.provider.taskDetail;

import com.huarui.dictionary.DictionaryEnum;
import com.huarui.dictionary.service.DictionaryReadOnlyService.DictionaryResource;
import com.huarui.purchaseOrder.model.PurOrSaleOrder;
import com.huarui.task.model.TaskDetail;
import com.huarui.task.service.provider.TaskDetailProvider;
import com.huarui.user.model.SessionMessage;
import com.huarui.user.security.UserHolder;

public class PurOrSaleSaveTaskDetailProviderImpl implements TaskDetailProvider{

	private PurOrSaleOrder orders;
	
	private TaskDetail last;
	
	
	public PurOrSaleSaveTaskDetailProviderImpl(PurOrSaleOrder orders, TaskDetail last) {
		super();
		this.orders = orders;
		this.last = last;
	}
	
	@Override
	public TaskDetail createTaskDetail() {
		return saveTaskDetail(last, orders);
	}
	
	private TaskDetail saveTaskDetail(TaskDetail last, PurOrSaleOrder orders) {
		
		TaskDetail taskDetail = new TaskDetail();
		
		taskDetail.setRemark(null);
		taskDetail.setGmtAction(null);
		
		SessionMessage sm = UserHolder.getCurrentSessionMessage();
		taskDetail.setActorId(sm.getUserId());
		taskDetail.setActorName(sm.getUserName());
		
		taskDetail.setActionCompanyId(sm.getCompanyId());
		taskDetail.setActionCompanyName(sm.getCompanyName());
		
		taskDetail.setEndStatus(Integer.parseInt(orders.getStatus()));
		taskDetail.setEndStatusId(Integer.parseInt(orders.getStatus()));
		String endStatusName = DictionaryResource.getDictionaryValue(DictionaryEnum.GROUP_ORDER_STATUS, orders.getStatus());
		taskDetail.setEndStatusName(endStatusName);
		
		if(last != null) {
			taskDetail.setPreviouseId(last.getId());
			taskDetail.setInitStatus(last.getEndStatus());
			taskDetail.setInitStatusId(last.getEndStatusId());
			taskDetail.setInitStatusName(last.getEndStatusName());
		} else {
			taskDetail.setPreviouseId(null);
			taskDetail.setInitStatus(Integer.parseInt(PurOrSaleOrder.STATUS_INITIAL));
			taskDetail.setInitStatusId(Integer.parseInt(PurOrSaleOrder.STATUS_INITIAL));
			taskDetail.setInitStatusName(DictionaryResource.getDictionaryValue(DictionaryEnum.GROUP_ORDER_STATUS, PurOrSaleOrder.STATUS_INITIAL));
			
//			if(PurOrSaleOrder.STATUS_WAIT_SELLER_ACPT.equals(orders.getStatus()) || PurOrSaleOrder.STATUS_WAIT_BUYER_ACPT.equals(orders.getStatus())) {
//				taskDetail.setInitStatus(Integer.parseInt(PurOrSaleOrder.STATUS_TMP));
//				taskDetail.setInitStatusId(Integer.parseInt(PurOrSaleOrder.STATUS_TMP));
//				taskDetail.setInitStatusName(DictionaryResource.getDictionaryValue(DictionaryEnum.GROUP_ORDER_STATUS, PurOrSaleOrder.STATUS_TMP));
//			}
		}
		
		taskDetail.setActionId(orders.getActionId());
		taskDetail.setActionName(orders.getActionName());
		
		if(last == null) {
			taskDetail.setSequence(1);
		} else {
			taskDetail.setSequence(last.getSequence() + 1);
		}
		
		return taskDetail;
	}

}
